package phone.example.com.a36kr.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import phone.example.com.a36kr.R;
import phone.example.com.a36kr.bean.InvestBean;
import phone.example.com.a36kr.nettool.NetTool;

/**
 * Created by dllo on 16/8/17.
 */
public class InvestListViewAdapter extends BaseAdapter {
    private Context mContext;
    private InvestBean investBean;


    public InvestListViewAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setInvestBean(InvestBean investBean) {
        this.investBean = investBean;
    }

    @Override
    public int getCount() {
        return investBean != null ? investBean.getData().getData().size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.invest_items_listview, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        NetTool netTool = new NetTool();
        netTool.getImage(investBean.getData().getData().get(position).getCompany_logo(), viewHolder.companyLogo);
        viewHolder.companyName.setText(investBean.getData().getData().get(position).getCompany_name());
        viewHolder.companyBrief.setText(investBean.getData().getData().get(position).getCompany_brief());
        netTool.getImage(investBean.getData().getData().get(position).getFile_list_img(), viewHolder.fileListImg);
        viewHolder.leadnameFirst.setText(investBean.getData().getData().get(position).getLead_name());
        viewHolder.adnameSecond.setText(investBean.getData().getData().get(position).getCf_advantage().get(0).getAdname());
        viewHolder.adcontentSecond.setText(investBean.getData().getData().get(position).getCf_advantage().get(0).getAdcontent());
        viewHolder.adnameThird.setText(investBean.getData().getData().get(position).getCf_advantage().get(1).getAdname());
        viewHolder.adcontentThird.setText(investBean.getData().getData().get(position).getCf_advantage().get(1).getAdcontent());
        viewHolder.fundingStatus.setText(investBean.getData().getData().get(position).getFundStatus().getDesc());
        viewHolder.investItemDesc.setText(investBean.getData().getData().get(position).getFundStatus().getDesc());
        Log.d("InvestListViewAdapter", investBean.getData().getData().get(position).getFundStatus().getDesc());
        // 进度百分比
        int progress = (int) (investBean.getData().getData().get(position).getRate() * 100);
        viewHolder.investItemDesc.setText(progress + "%");
        // seekBar 进度条
        if (progress < 100) {
            viewHolder.investSeekBar.setProgress(progress);
        } else if (progress >= 100) {
            viewHolder.investSeekBar.setProgress(progress);
//            viewHolder.investSeekBar.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        }

        if (progress >= 80) {
            Button btnInvestGo = (Button) convertView.findViewById(R.id.invest_items_buy);
            btnInvestGo.setText("去看看");
            btnInvestGo.setTextColor(Color.WHITE);
            btnInvestGo.setBackgroundColor(Color.rgb(192,192,192));
        }


        return convertView;
    }

    private class ViewHolder {

        private final ImageView companyLogo;
        private final TextView companyName;
        private final TextView companyBrief;
        private final ImageView fileListImg;
        private final TextView leadnameFirst;
        private final TextView adnameSecond;
        private final TextView adcontentSecond;
        private final TextView adnameThird;
        private final TextView adcontentThird;
        private final TextView fundingStatus;
        private final TextView investItemDesc;
        private final SeekBar investSeekBar;

        public ViewHolder(View convertView) {

            companyLogo = (ImageView) convertView.findViewById(R.id.company_logo);//头像
            companyName = (TextView) convertView.findViewById(R.id.company_name);//名称
            companyBrief = (TextView) convertView.findViewById(R.id.company_brief);//介绍
            fileListImg = (ImageView) convertView.findViewById(R.id.file_list_img);//中间介绍大图
            leadnameFirst = (TextView) convertView.findViewById(R.id.lead_name_first);//公司信息:领头方公司信息
            adnameSecond = (TextView) convertView.findViewById(R.id.invest_items_adname_second);//公司信息:第二行第一个(创始人,孵化器)
            adcontentSecond = (TextView) convertView.findViewById(R.id.invest_items_adcontent_second);//公司信息:第二行第二个(创始人名称)
            adnameThird = (TextView) convertView.findViewById(R.id.invest_items_adname_third);//第三行第第一个
            adcontentThird = (TextView) convertView.findViewById(R.id.invest_items_adcontent_third);//第三个第二个
            fundingStatus = (TextView) convertView.findViewById(R.id.crowd_funding_status);//募资情况
            investItemDesc = (TextView) convertView.findViewById(R.id.invest_items_desc);//募资百分比
            investSeekBar = (SeekBar) convertView.findViewById(R.id.invest_items_seekbar_rate);//募资进度条

        }
    }
}
